/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.link.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.dao.gen.bean.BizLink;
import com.smooth.dao.gen.BizLinkMapper;

import com.smooth.site.sx.mag.biz.link.service.BizLinkService;

/**
 * 友情链接管理Controller
 * @author bm
 * @version 2019-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/link/bizLink")
public class BizLinkController extends BaseController {
	Logger log = LoggerFactory.getLogger(BizLinkController.class);
	@Autowired
	private BizLinkService bizLinkService;
	
	@ModelAttribute
	public BizLink get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizLink entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizLinkService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizLink();
		}
		return entity;
	}
	
	@RequiresPermissions("link:bizLink:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizLink bizLink, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizLink> page = bizLinkService.findPage(new Page<BizLink>(request, response), bizLink); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/link/bizLinkList";
		}
		return "biz/link/bizLinkList";
	}
	
	@RequiresPermissions("link:bizLink:view")
	@RequestMapping(value = "form")
	public String form(BizLink bizLink, Model model) {
		model.addAttribute("bizLink", bizLink);
		return "biz/link/bizLinkForm";
	}

	@RequiresPermissions("link:bizLink:edit")
	@RequestMapping(value = "save")
	public String save(BizLink bizLink, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizLink)){
			return form(bizLink, model);
		}
		try{
			bizLinkService.save(bizLink);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/link/bizLink?repage";
		}
		addMessage(redirectAttributes, "保存友情链接管理成功");
		return "redirect:"+Global.getAdminPath()+"/link/bizLink?repage";
	}
	
	@RequiresPermissions("link:bizLink:edit")
	@RequestMapping(value = "delete")
	public String delete(BizLink bizLink, RedirectAttributes redirectAttributes) {
		try{
			bizLinkService.delete(bizLink);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/link/bizLink?repage";
		}
		addMessage(redirectAttributes, "删除友情链接管理成功");
		return "redirect:"+Global.getAdminPath()+"/link/bizLink?repage";
	}

}